package com.zyong.design.pattern.flyweight.case3;

import java.util.HashMap;
import java.util.Map;

/**
 * @program: zyong-design-pattern
 * @ClassName: PieceFactory
 * @author: zyong
 * @create: 2024-04-06 19:36
 */
public class PieceFactory {
    private static Map<String, Piece> map = new HashMap<>();

    public static Piece getPiece(String color) {
        if (!map.containsKey(color)) {
            Piece piece;

            switch (color) {
                case "black":
                    piece = new BlackPiece();
                    break;
                case "white":
                    piece = new WhitePiece();
                    break;
                default:
                    throw new IllegalArgumentException("Unsupported color " + color);
            }

            map.put(color, piece);
        }

        return map.get(color);
    }
}
